package util;

import java.beans.IntrospectionException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import net.pimpas.util.clone.CloneUtils;
import net.pimpas.util.reflection.ReflectionUtil;

public class BeanUtil implements Serializable {

	private static final long serialVersionUID = -2203830524913563243L;
	
	private Integer id;
	private String nome;
	private TesteSubClass subclass = new TesteSubClass();
	
	
	public TesteSubClass getSubclass() {
		return subclass;
	}

	public void setSubclass(TesteSubClass subclass) {
		this.subclass = subclass;
	}

	public static void main(String[] args) {
		Map<String, Object> values = new HashMap<String, Object>();
		
		BeanUtil beanUtil = new BeanUtil();
		values.put("id", 3);
		values.put("nome", "Paulo Cesar da Silva Reis");
		values.put("subclass.descricao", "EH NOIS NA FITA!");
		values.put("subclass.util", beanUtil);
		try {
			ReflectionUtil.fillBean(values, beanUtil);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(beanUtil.getSubclass().getDescricao());
		System.out.println(beanUtil.getSubclass().getUtil().getId());
		System.out.println(beanUtil);
		System.out.println(CloneUtils.clone(beanUtil));
		System.out.println(beanUtil.getSubclass());
		System.out.println(CloneUtils.clone(beanUtil.getSubclass()));
	}

	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public String getNome() {
		return nome;
	}


	public void setNome(String nome) {
		this.nome = nome;
	}
	public static final class TesteSubClass implements Cloneable, Serializable {
		
		private static final long serialVersionUID = -6830324846704868401L;
		private String descricao;
		private BeanUtil util;

		public String getDescricao() {
			return descricao;
		}

		public void setDescricao(String descricao) {
			this.descricao = descricao;
		}

		public BeanUtil getUtil() {
			return util;
		}

		public void setUtil(BeanUtil util) {
			this.util = util;
		}
		
		@Override
		public Object clone() throws CloneNotSupportedException {
			TesteSubClass sub = new TesteSubClass();
			sub.setDescricao(this.getDescricao());
			sub.setUtil(this.getUtil());
			
			return sub;
		}
	}
}